from qiniu import Auth, put_file, etag
from qiniu_config import access_key, secret_key, bucket_name

from rest_framework.response import Response
from rest_framework.views import APIView

q = Auth(access_key, secret_key)


class ImgUploadView(APIView):
    def get(self, request, **kwargs):
        user = request.user
        if user.qiniu_token:
            return Response({'token': user.qiniu_token})
        else:
            key = None
            token = q.upload_token(bucket_name, key, 3600 * 24 * 30)
            user.qiniu_token = token
            user.save()
        return Response({'token': token})


class ImgUploadRefreshView(APIView):
    def get(self, request, **kwargs):
        key = None
        user = request.user
        token = q.upload_token(bucket_name, key ,3600 * 24 * 30)
        user.qiniu_token = token
        user.save()
        return Response({'token': token})

